'use strict';
angular.module('gkwb')
    .controller('PromiseCtrl', ['$http', '$scope', '$state', 'Dict','$interval', function($http, $scope, $state, Dict,$interval) {
        Dict.queryProv();
        Dict.queryNation();
        Dict.queryForeign();
        Dict.queryStuType();
        Dict.querySports();
        Dict.querySpecialty();
        Dict.queryPolity();
        Dict.queryGraduation();
        Dict.queryCityByProvId(42);
        $scope.time = 10;
        $scope.enabled = false;
        $interval(function(){
            $scope.time--;
            if($scope.time == 0){
                $scope.time = '';
                $scope.enabled = true;
            }
        },1000,10);

    }])
    .controller('ExamCtrl', ['$http','$rootScope' ,'$scope', '$state', 'Student', '$window', 'Dict','$stateParams', function($http,$rootScope, $scope, $state, Student, $window, Dict,$stateParams) {
        $scope.opt = $stateParams.opt;
        $scope.state = false;
        $scope.spokenTypes = [{
            id: 1,
            name: '参加2016年4月份考试'
        }, {
            id: 2,
            name: '用以往成绩'
        }, {
            id: 3,
            name: '不参加考试'
        }];
        
        var examId = $window.sessionStorage.token;
        $scope.student = null;
        $scope.loading = Student.get(examId).then(function(data) {
            $rootScope.hide = true;
            $rootScope.student = $scope.student = data;
            $scope.master = angular.copy(data);
            if (data) {
                loadNation();
                loadForeign();
                loadStuType();
                if (data.subjectType == 'ART')
                    loadSpec();
                if(data.subjectType == 'SPORT')
                    loadSports();
                loadProv();
                loadClass();
                loadPolity();
                loadGraduation();

                if (data.censusProv) {
                    $scope.loadCity(data.censusProv);
                }
                if (data.censusCity) {
                    $scope.loadRegion(data.censusCity);
                }
                if (data.gradeSchoolCity) {
                    $scope.loadSchoolRegion(data.gradeSchoolCity);
                }
                if (data.gradeSchoolRegion) {
                    $scope.loadSchool(data.gradeSchoolRegion);
                }
            }

        },function(err){
            $scope.loading = null;
            alert(err.message);
        })

        function loadNation() {
            Dict.queryNation().then(function(data) {
                $scope.nations = data;
            })
        }

        function loadSports(){
            Dict.querySports().then(function(data){
                $scope.sports = data;
            })
        }

        function loadForeign() {
            Dict.queryForeign().then(function(data) {
                $scope.foreigns = data;
            })
        }

        function loadStuType() {
            Dict.queryStuType().then(function(data) {
                $scope.stuTypes = data;
            })
        }

        function loadPolity() {
            Dict.queryPolity().then(function(data) {
                $scope.polities = data;
            })
        }

        function loadGraduation() {
            Dict.queryGraduation().then(function(data) {
                $scope.graduations = data;
            })
        }

        function loadSpec() {
            Dict.querySpecialty().then(function(data) {
                if ($scope.student && $scope.student.arts.length == 0) {
                    angular.forEach(data, function(value) {
                        if (value.id == 4) {
                            Dict.querySpecDirect(value.id).then(function(data) {
                                value.directs = data;
                                $scope.student.arts.push(value);
                            });
                        } else {
                            $scope.student.arts.push(value);
                        }
                    });
                }
            });
        }

        function loadSchoolCity() {
            Dict.queryCityByProvId(42)
                .then(function(data) {
                    $scope.schoolCities = data;
                })
        }
        loadSchoolCity();

        $scope.loadSchoolRegion = function(city) {
            if (city) {
                Dict.queryRegionByCityId(city.id)
                    .then(function(data) {
                        $scope.schoolRegions = data;
                    });
            }
        }
        $scope.loadSchool = function(region) {
            if (region) {
                $scope.loading = Dict.querySchoolByRegionId(region.id)
                    .then(function(data) {
                        $scope.schools = data;
                    });
            }
        }

        function loadProv() {
            $scope.loading = Dict.queryProv().then(function(data) {
                $scope.provs = data;
            });

        }

        $scope.loadCity = function(prov) {
            if (prov && prov.id == 42) {
                Dict.queryCityByProvId(prov.id)
                    .then(function(data) {
                        $scope.cities = data;
                    });
            }
            else{
                $scope.student.censusCity = null;
                $scope.student.censusRegion = null;
            }
        }

        $scope.loadRegion = function(city) {
            if (city) {
                Dict.queryRegionByCityId(city.id)
                    .then(function(data) {
                        $scope.regions = data;
                    });
            }
        }

        function loadClass() {
            $scope.classes = Dict.queryClass();
        }

        $scope.specSigned = true;
        $scope.specChange = function(item) {
            if (item.id == 9) {
                if (item.isEnabled) {
                    angular.forEach($scope.student.arts, function(value) {
                        if (value.id != 9) {
                            value.isEnabled = false;
                        }
                    });
                    $scope.specSigned = false;
                } else {

                    $scope.specSigned = true;
                }
            } 
            else {
                angular.forEach($scope.student.arts, function(value) {
                        if (value.id == 9) {
                            value.isEnabled = false;
                            angular.forEach(value.directs,function(d){
                                d.isEnabled = false;
                            })
                        }
                    });
            }
            if(item.id ==  4){
                angular.forEach(item.directs,function(d){
                                d.isEnabled = false;
                            })
            }
            
        }

        $scope.sportChange = function(isSigned){
            if(isSigned == 2){
                $scope.student.sport = null;
            }
        }

        $scope.addFamily = function() {
            var f = {
                relation: {
                    id: null,
                    name: null
                },
                name: null
            };
            $scope.student.family.push(f);
        }

        $scope.goNext = function() {
            $state.go('main.pay');
        }

        $scope.submit = function(form) {
            if($stateParams.opt){
                if(angular.equals($scope.master,$scope.student)){
                    return $state.go('home.exam');
                }
            }
            if (form.$invalid) {
                $scope.submitted = true;
                alert('有未填项或填写格式错误的内容，请检查红色框内的项目！');
                return;
            }
            if ($scope.student.subjectType == 'ART' && $scope.student.arts[0].isEnabled) {
                var f = $window.confirm('确定不参加艺术专业报考吗？');
                if (!f) {
                    return;
                }
            }
            if ($scope.student.subjectType == 'SPORT' && $scope.student.sportSigned == 2) {
                var f = $window.confirm('确定不参加体育专业素质测试吗？');
                if (!f) {
                    return;
                }
            }
            var birth = $scope.student.certino.substring(6,14);
            $scope.student.birth = birth;
            $scope.state = true;
            $scope.loading = Student.save($scope.student)
                .then(function(res) {
                        $window.sessionStorage.isReg = true;
                        $scope.state = false;
                        alert('提交成功！');
                        /**
                        if(res.data.pay && !res.data.pay.paid){
                            $state.go('home.pay');
                        }
                        else{
                            $state.go('home.exam');
                        }
                        **/
                        $state.go('home.exam');
                    },
                    function(err) {
                        $scope.state = false;
                        alert(err);
                    });
        }
    }]);